Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

274
Views
Considering we already have `setOrigin`, what is "Phaser.Display.Align.In.Center" used for?

I'm confused about the use case of Phaser.Display.Align.In.Center, the following code is adapted from an official example

class Example extends Phaser.Scene
{
  constructor ()
  {
    super();
  }
  preload() {
    this.load.path = 'https://raw.githubusercontent.com/photonstorm/phaser3-examples/master/public/assets/';
    this.load.image('pic', 'pics/barbarian-loading.png');
  }
  create ()
  {
    const pic = this.add.image(400, 300, 'pic');
    //Phaser.Display.Align.In.Center(pic, this.add.zone(400, 300, 800, 600));
  }
}
var config = {
  width: 800,
  height: 600,
backgroundColor: '#666', //0xf3f3f3
  scene: [Example]
}

var game = new Phaser.Game(config);
<script src="https://cdn.jsdelivr.net/npm/phaser@3.55.2/dist/phaser.js"></script>

where I've commented this line out

//Phaser.Display.Align.In.Center(pic, this.add.zone(400, 300, 800, 600));

No matter if I use this line, I get the exactly same result, a centered image.

Considering we already have setOrigin, what is "Phaser.Display.Align.In.Center" used for?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The Phaser.Display.Align is used for positioning objects relative to another objects. setOrigin is just used to define from where the coordinats of the object should be calculated.

In this example you can see how you can position an object relative to another (with Phaser.Display.Align), but without knowing/setting the coordinates for it.

It is good/used for, positioning to images on top/next of eachother like clothes, guns, text on items, ...

On each click, the green rectangle will be repositioned, with Phaser.Display.Align.
(I'm using both Phaser.Display.Align.In and Phaser.Display.Align.To to illustrate the difference ).

function create () {
    this.add.text(10,10, 'Click red Cube', '#ffffff')
    rect1 = this.add.rectangle(200, 100, 50, 50, 0xff0000);
    rect2 = this.add.rectangle(-50, -50, 30, 30, 0x00ff00);
    
    Phaser.Display.Align.To.TopCenter(rect2, rect1);
    
    rect1.setInteractive();
    rect1.on('pointerdown', alignObject)
    
  }
  
 function alignObject(){
    switch(alignObjPoistion){
      case 0:
        Phaser.Display.Align.In.TopCenter(rect2, rect1);
        break;
      case 1:
        Phaser.Display.Align.In.Center(rect2, rect1);
        break;
      case 2:
        Phaser.Display.Align.In.BottomCenter(rect2, rect1);
        break;
      case 3:
        Phaser.Display.Align.To.BottomCenter(rect2, rect1);
        break;
      case 4:
        Phaser.Display.Align.To.TopCenter(rect2, rect1);

        alignObjPoistion = -1;
    }
    alignObjPoistion++;
 }
 
var alignObjPoistion = 0;
var rect1; 
var rect2; 

var config = {
  width: 400,
  height: 200,
  scene: { create }
}

var game = new Phaser.Game(config);
<script src="https://cdn.jsdelivr.net/npm/phaser@3.55.2/dist/phaser.js"></script>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!